//
//  FeedBackVC.m
//  ImGuider
//
//  Created by llt on 2017/6/12.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "FeedBackVC.h"
#import <IQTextView.h>

@interface FeedBackVC ()

@property (weak, nonatomic) IBOutlet UILabel *showTypeLabel;

@property (weak, nonatomic) IBOutlet UIButton *adviceBtn;
@property (weak, nonatomic) IBOutlet UIButton *bugBtn;

@property (weak, nonatomic) IBOutlet UIButton *contentBug;
@property (nonatomic) NSInteger chooseType;
@property (weak, nonatomic) IBOutlet IQTextView *contentTV;
@property (weak, nonatomic) IBOutlet UILabel *wordCountLabel;
@property (weak, nonatomic) IBOutlet UITextField *contactTF;

@end

@implementation FeedBackVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isHiddenPlayStatusButton = YES;
    [self setup];
}

- (void)setup {
    
    self.title = MYLocalizedString(@"SETTING_FEEDBACK", nil);
    self.view.backgroundColor = kBGColor;
    
    [self barItemWithNameArray:@[MYLocalizedString(@"PUBLIC_COMMENT_COMMIT", nil)] colorArray:@[[UIColor blackColor]] selArray:@[@"commitFeedBack"] isLeft:NO];
    
    self.showTypeLabel.text = MYLocalizedString(@"FEEDBACK_SELECTTYPE", nil);
    [self.adviceBtn setTitle:MYLocalizedString(@"FEEDBACK_PRODUCT", nil) forState:UIControlStateNormal];
    [self.bugBtn setTitle:MYLocalizedString(@"FEEDBACK_BUG", nil) forState:UIControlStateNormal];
    [self.contentBug setTitle:MYLocalizedString(@"FEEDBACK_CONTENT", nil) forState:UIControlStateNormal];
    
    self.contentTV.placeholder = MYLocalizedString(@"FEEDBACK_CONTENT_PLACEHOLDER", nil);
    self.contactTF.placeholder = MYLocalizedString(@"FEEDBACK_MOBILE_PLACEHOLDER", nil);
    
    self.adviceBtn.selected = YES;
    
    self.adviceBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    self.adviceBtn.titleLabel.minimumScaleFactor = 0.5;
    self.adviceBtn.titleLabel.numberOfLines = 0;
    
    self.bugBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    self.bugBtn.titleLabel.minimumScaleFactor = 0.5;
    self.bugBtn.titleLabel.numberOfLines = 0;
    
    self.contentBug.titleLabel.adjustsFontSizeToFitWidth = YES;
    self.contentBug.titleLabel.minimumScaleFactor = 0.5;
    self.contentBug.titleLabel.numberOfLines = 0;
    
    
//    [self.contactTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventValueChanged];
}

- (void)commitFeedBack {
    
    NSString *content = self.contentTV.text;
    
    if (content.length == 0) {
        [IGUtil showToast:MYLocalizedString(@"FEEDBACK_CONTENT_PLACEHOLDER", nil)];
        return;
    }
    
    NSString *contact = self.contactTF.text;
    
    if (contact.length == 0) {
        [IGUtil showToast:MYLocalizedString(@"FEEDBACK_MOBILE_PLACEHOLDER", nil)];
        return;
    }
    
    [NetAPIManager feedback:@(self.chooseType) content:content account:contact Block:^(NSString *result, NSError *error) {
        
        if (!error) {
            
            [IGUtil showToast:MYLocalizedString(@"PUBLIC_COMMIT_SUCCESS", nil)];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

#pragma mark - UITextViewDelegate
#define MAX_STARWORDS_LENGTH 200

- (void)textViewDidChange:(UITextView *)textView {
    
    NSString *toBeString = textView.text;
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position) {
        
        if (toBeString.length > MAX_STARWORDS_LENGTH) {
            
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];
            if (rangeIndex.length == 1) {
                
                textView.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];
            } else  {
                
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];
                textView.text = [toBeString substringWithRange:rangeRange];
            }
        }
        
        self.wordCountLabel.text = @(MAX_STARWORDS_LENGTH - textView.text.length).stringValue;
    }
}


- (IBAction)chooseStyleAction:(UIButton *)sender {
    
    self.adviceBtn.selected = NO;
    self.contentBug.selected = NO;
    self.bugBtn.selected = NO;
    sender.selected = YES;
    
    self.chooseType = sender.tag;
}

@end
